<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Extended Setup</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="installation.html" title="Chapter 2. Installation">
<link rel="prev" href="installing.smarty.basic.html" title="Basic Installation">
<link rel="next" href="smarty.for.designers.html" title="Part II. Smarty For Template Designers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Extended Setup</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="installing.smarty.basic.html">Prev</a> </td>
<th width="60%" align="center">Chapter 2. Installation</th>
<td width="20%" align="right"> <a accesskey="n" href="smarty.for.designers.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Extended Setup">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="installing.smarty.extended"></a>Extended Setup</h2></div></div></div>
<p>
    This is a continuation of the <a class="link" href="installing.smarty.basic.html" title="Basic Installation">basic installation</a>, please read
    that first!
   </p>
<p>
    A slightly more flexible way to setup Smarty is to
    <a class="ulink" href="http://php.net/ref.classobj" target="_top">extend the class</a> and
    initialize your Smarty environment. So instead of repeatedly setting
    directory paths, assigning the same vars, etc., we can do that in one place.
    </p>
<p>
    Lets create a new directory <code class="filename">/php/includes/guestbook/</code>
     and make a new file called <code class="filename">setup.php</code>. In our example
     environment, <code class="filename">/php/includes</code> is in our
     <code class="literal">include_path</code>.
     Be sure you set this up too, or use absolute file paths.
   </p>
<div class="example">
<a name="id342330"></a><p class="title"><b>Example 2.10. /php/includes/guestbook/setup.php</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;?php

// load Smarty library
require('Smarty.class.php');

// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');

class Smarty_GuestBook extends Smarty {

   function __construct()
   {

        // Class Constructor.
        // These automatically get set with each new instance.

        parent::__construct();

        $this-&gt;setTemplateDir('/web/www.example.com/guestbook/templates/');
        $this-&gt;setCompileDir('/web/www.example.com/guestbook/templates_c/');
        $this-&gt;setConfigDir('/web/www.example.com/guestbook/configs/');
        $this-&gt;setCacheDir('/web/www.example.com/guestbook/cache/');

        $this-&gt;caching = Smarty::CACHING_LIFETIME_CURRENT;
        $this-&gt;assign('app_name', 'Guest Book');
   }

}
?&gt;

    </pre></div>
</div>
<br class="example-break"><p>
   Now lets alter the <code class="filename">index.php</code> file to use
   <code class="filename">setup.php</code>:
  </p>
<div class="example">
<a name="id342372"></a><p class="title"><b>Example 2.11. /web/www.example.com/guestbook/htdocs/index.php</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;?php

require('guestbook/setup.php');

$smarty = new Smarty_GuestBook();

$smarty-&gt;assign('name','Ned');

$smarty-&gt;display('index.tpl');
?&gt;

   </pre></div>
</div>
<br class="example-break"><p>
   Now you see it is quite simple to bring up an instance of Smarty, just use
   <code class="literal">Smarty_GuestBook()</code> which automatically initializes everything for our
   application.
  </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="installing.smarty.basic.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="installation.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="smarty.for.designers.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Basic Installation </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Part II. Smarty For Template Designers</td>
</tr>
</table>
</div>
</body>
</html>
